package	hottargui.domain.standard;

import hottargui.domain.*;

import java.util.*;

/** AlphaGame implementation.
    Presently simply a stub.
 */

public class StandardGame implements Game {

	private Board _board;
	private StateMachine _stm;
	private GameFactory _gameFactory;
	private GameListener listener = null;
	

	// Til brug for factory
	public StandardGame(GameFactory gameFactory) {
		_gameFactory = gameFactory;
		
		
		
		//_objectServer = StandardObjectServerSingleton.getInstance(_gameFactory);
		 //_board =_objectServer.getBoard();
		//_stm = StandardStateMachineSingleton.getInstance(_objectServer.getCurrentFactory());

		
		_stm = StandardStateMachineSingleton.getInstance(gameFactory);
		_board = _stm.getBoard();
	}

	public void newGame() {
		_stm.initialization(_gameFactory);
		_board = _stm.getBoard();
		//updateTiles();
	}

	/** return a specific tile */
	public Tile getTile( Position p ) {
		return _board.getTile(p);
	}

	public Player getPlayerInTurn() {
		return _stm.getPlayerInTurn();
	}

	public State getState() {
		return _stm.getState();
	}

	public boolean move(Position from, Position to, int count) {
		boolean legal;
		legal = _stm.move(from, to, count);
		return legal;
	}

	public boolean buy(int count, Position deploy) {
		// System.out.println(deploy);
		boolean legal = _stm.buy(count, deploy);
		return legal;
	}

	public PlayerColor turnCard() {
		PlayerColor pc = _stm.turnCard();
		return pc;
	}

	public boolean rollDieAllowed() {
		return _stm.rollDieAllowed();
	}


	public void rollDie() {
		_stm.rollDie();
	}

	public int getDieValue() {
		return(_stm.getDieValue());
	}

	public Iterator<Tile> getBoardIterator() {	  
		Iterator<? extends Tile> imt = _board.getBoardIterator();
		Vector<Tile> board = new Vector<Tile>();
		while (imt.hasNext()) {
			Tile t = imt.next();
			board.addElement(t);
		}
		return board.iterator();
	}

	public void addGameListener( GameListener observer ) {
		if ( listener != null ) 
			throw new RuntimeException("There is only supported one listener." );
		listener = observer;
		_stm.addGameListener(listener);
	}

	public String getTitle() {
		return _gameFactory.getWindowTitle();
	}

	public void endAttack() {
		_stm.setState(State.buy,false);
	}
}
